package com.anmai.crawler1.service;

import com.anmai.crawler1.dao.ImgDao;
import com.anmai.crawler1.entity.ImgBean;
import com.anmai.crawler1.utils.JsoupUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sound.midi.Soundbank;
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.UUID;

/**
 * Created by CJ on 2018-07-05.
 */
public class PhotoCrawler extends CrawlerServiceTest {

    @Autowired
    private ImgDao imgDao;

    @Test
    public void getLocation() throws IOException, InterruptedException {
       for(int i=0;i<29;i++){
           BufferedInputStream accept = JsoupUtils.getConnection("http://photo2018.usts.edu.cn/?P="+i)
                   .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
                   .header("Accept-Encoding", "gzip, deflate")
                   .header("Accept-Language", "zh-CN,zh;q=0.9")
                   .header("Cache-Control", "max-age=0")
                   .execute().bodyStream();
            Thread.sleep(100);
           Document doc = Jsoup.parse(accept, "gb2312", "http://photo2018.usts.edu.cn/?P=0");
           Elements elements = doc.select("body table tbody tr td table tbody tr td");
           //遍历tr
           for(Element e : elements){
               if(e.selectFirst("a") != null) {
                   String url =  e.selectFirst("a").attr("href").replaceFirst("\\\\", "");
                   ImgBean imgBean = new ImgBean();
                   imgBean.setImg(url);
                   imgBean.setStatus(0);
                   //判断是否存在
                   if(imgDao.isExist(url) == 0){
                       imgDao.add(imgBean);
                   }
               }
           }

       }
    }

    //爬取图片
    @Test
    public void downloadImg() throws Exception {
//        downloadPicture(url);
        List<ImgBean> imgBeans = imgDao.queryByStatus(0);
        imgBeans.stream().forEach(imgBean -> {
            //下载图片
            //图片的保存路径
            try {
                String[] split = imgBean.getImg().split("\\\\");
                String filePath="f://imgs/"+ split[0]+"/"+imgBean.getId() +"_"+split[1]+".jpg";
                String url = "http://photo2018.usts.edu.cn/"+URLEncoder.encode(split[0],"utf-8")+"/"
                        +split[1];
                getImage(url,filePath);
                //更新状态
                imgBean.setStatus(1);
                imgDao.update(imgBean);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        });
    }


    @Test
    public void hello() throws Exception {
//        download("http://photo2018.usts.edu.cn/20号位（夏健）/DSC_7080.JPG", "51bi.jpg","c:\\");
        String urlpath = "http://photo2018.usts.edu.cn/01%E5%8F%B7%E4%BD%8D%EF%BC%88%E5%87%8C%E9%83%81%E4%B9%8B%EF%BC%89/002.JPG";
        //图片的url
        String filePath="f://myfist.jpg";
        //图片的保存路径
        try {
            getImage(urlpath,filePath);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void getImage(String urlpath,String filePath) throws Exception{  //首先得到请求路径,并抛出异常


        URL url = new URL(urlpath);  //建立url 类对象，抛异常
        HttpURLConnection conn =(HttpURLConnection) url.openConnection(); //得到urlConnection对象
        conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36");

        conn.setRequestMethod("GET");     //声明请求方式
        conn.setConnectTimeout(60000*1000);    //设置连接超时
        if(conn.getResponseCode() == 200){
            InputStream inputStream = conn.getInputStream();   //得到一个输入流

            //从服务端返回数据，相对客户端为流入
            byte[] data= readInstream(inputStream);  //得到数据
            File file = new File(filePath);   //创建保存文件
            File fileParent = file.getParentFile();
            if(!fileParent.exists()){
                fileParent.mkdirs();
            }
            FileOutputStream outputStream = new FileOutputStream(file);//创建一个文件的输出流
            outputStream.write(data);  //把所有的二进值数据全部写入到我们建好的文件中
            outputStream.flush();
            outputStream.close();   //关闭输出流
//            System.out.println("下载完成！");
        }
    }


    //读取流文件的内容
    private static byte[] readInstream(InputStream inputStream) throws Exception {

        ByteArrayOutputStream byteArrayOutputStream =
                new ByteArrayOutputStream(); //创建ByteArrayOutputStream 对象
        byte[] buffer = new byte[1024];   //声明缓冲区
        int length = -1;         //定义读取默认长度

        while((length=inputStream.read(buffer))!=-1){
            byteArrayOutputStream.write(buffer,0,length);
            //把缓存区中输出到内存中
        };
        byteArrayOutputStream.close();  //关闭输出流
        inputStream.close();          //关闭输入流

        return byteArrayOutputStream.toByteArray();
        //返回这个输出流的字节数组
    }
}
